package com.zjxf.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.zjxf.base.bean.po.PmsAsProductClassify;
import com.zjxf.base.bean.ro.RoPmsAsProductClassify;
import com.zjxf.base.bean.sort.SortDto;
import com.zjxf.base.bean.sort.SortUtils;
import com.zjxf.base.bean.vo.VoPmsAsProductClassify;
import com.zjxf.base.common.OperationException;
import com.zjxf.base.common.SysConst;
import com.zjxf.repository.PmsAsProductClassifyRepository;
import com.zjxf.service.PmsAsProductClassifyService;
import com.zjxf.utils.Conversion;
import com.zjxf.utils.DynamicSpecifications;
import com.zjxf.utils.PageUtils;
import com.zjxf.utils.SearchFilter;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * 农副产品分类
 *
 * @author ZhangWei
 * @date 2021/4/6/006 16:53
 */
@Service
@AllArgsConstructor
@Transactional(rollbackOn = RuntimeException.class)
public class PmsAsProductClassifyServiceImpl implements PmsAsProductClassifyService {

    private final PmsAsProductClassifyRepository pmsAsProductClassifyRepository;

    public Optional<PmsAsProductClassify> findById(Long id) {
        return pmsAsProductClassifyRepository.findByIdAndDeleteState(id, SysConst.DeleteState.UN_DELETE_STATE.getCode());
    }

    public List<PmsAsProductClassify> findByPId(Long pId) {
        return pmsAsProductClassifyRepository.findByPIdAndDeleteState(pId, SysConst.DeleteState.UN_DELETE_STATE.getCode());
    }

    @Override
    public List<RoPmsAsProductClassify.ListWithChild> roList(List<PmsAsProductClassify> pmsAsProductClassifyList) {
        return pmsAsProductClassifyList.stream().map(item->{
            return Conversion.poTurnRo(item, new RoPmsAsProductClassify.ListWithChild());
        }).collect(Collectors.toList());
    }

    @Override
    public void adminAddOrUpdate(VoPmsAsProductClassify.AdminAddOrUpdate vo) {
        Optional<PmsAsProductClassify> optionalPmsAsProductClassify = findById(vo.getId());
        PmsAsProductClassify pmsAsProductClassify;
        if (optionalPmsAsProductClassify.isPresent()) {
            pmsAsProductClassify = optionalPmsAsProductClassify.get();
            pmsAsProductClassify = Conversion.voTurnPo(vo, pmsAsProductClassify);
        } else {
            pmsAsProductClassify = Conversion.voTurnPo(vo, new PmsAsProductClassify());
        }
        pmsAsProductClassifyRepository.save(pmsAsProductClassify);
    }


    @Override
    public void sellAddOrUpdate(VoPmsAsProductClassify.SelladdOrUpdate vo) {
        Optional<PmsAsProductClassify> optionalPmsAsProductClassify = findById(vo.getId());
        PmsAsProductClassify pmsAsProductClassify;
        if(optionalPmsAsProductClassify.isPresent()){
            pmsAsProductClassify = optionalPmsAsProductClassify.get();
            pmsAsProductClassify = Conversion.voTurnPo(vo, pmsAsProductClassify);
        }else{
            pmsAsProductClassify = Conversion.voTurnPo(vo, new PmsAsProductClassify());
            pmsAsProductClassify.setNavStatus(SysConst.ShowState.HIDE.getCode());
            pmsAsProductClassify.setShowStatus(SysConst.ShowState.DISPLAY.getCode());
        }
        pmsAsProductClassifyRepository.save(pmsAsProductClassify);


    }

    @Override
    public void delete(Long id) throws OperationException {
        PmsAsProductClassify pmsAsProductClassify = findById(id).orElseThrow(() -> new OperationException(SysConst.DATA_NOT_FOND));
        pmsAsProductClassify.setDeleteState(SysConst.DeleteState.DELETE_STATE.getCode());
        pmsAsProductClassifyRepository.save(pmsAsProductClassify);
    }

    @Override
    public PmsAsProductClassify getEntity(Long id) throws OperationException {
        return findById(id).orElseThrow(() -> new OperationException(SysConst.DATA_NOT_FOND));

    }

    @Override
    public RoPmsAsProductClassify.Details getEntityRo(PmsAsProductClassify pmsAsProductClassify) {
        return Conversion.poTurnRo(pmsAsProductClassify, new RoPmsAsProductClassify.Details());
    }

    @Override
    public Page<PmsAsProductClassify> page(VoPmsAsProductClassify.filter filter) {
        List<SearchFilter> searchFilters = new ArrayList<>();
        if (StringUtils.isNotBlank(filter.getKeywords())) {
            searchFilters.add(new SearchFilter("name", SearchFilter.Operator.LIKE, filter.getKeywords()));
        }
        if (filter.getShowStatus() != null) {
            searchFilters.add(new SearchFilter("showStatus", SearchFilter.Operator.EQ, filter.getShowStatus()));
        }
        if (filter.getNavStatus() != null) {
            searchFilters.add(new SearchFilter("navStatus", SearchFilter.Operator.EQ, filter.getNavStatus()));
        }
        if (filter.getPId() != null) {
            searchFilters.add(new SearchFilter("pId", SearchFilter.Operator.EQ, filter.getPId()));
        }
        Specification<PmsAsProductClassify> specification = DynamicSpecifications.bySearchFilterByBasic(searchFilters, filter.getStartTime(), filter.getEndTime());
        Sort orders = SortUtils.basicSort(
                new SortDto(SysConst.SortOrder.DESC.getCode(), "navStatus"),
                new SortDto(SysConst.SortOrder.DESC.getCode(), "showStatus"),
                new SortDto(SysConst.SortOrder.DESC.getCode(), "createTime"));
        return pmsAsProductClassifyRepository.findAll(specification, PageUtils.buildPageRequest(filter.getPageNum(), filter.getPageSize(), orders));

    }

    @Override
    public JSONObject pageRo(Page<PmsAsProductClassify> pmsAsProductClassifyPage) {
        List<PmsAsProductClassify> pageList = pmsAsProductClassifyPage.getContent();
        List<RoPmsAsProductClassify.Page> roList = pageList.stream().map(item -> {
            RoPmsAsProductClassify.Page page = Conversion.poTurnRo(item, new RoPmsAsProductClassify.Page());
            return page;
        }).collect(Collectors.toList());
        return PageUtils.page(pmsAsProductClassifyPage, roList);
    }

    @Override
    public List<PmsAsProductClassify> getList() {
        List<SearchFilter> searchFilters = new ArrayList<>();
        searchFilters.add(new SearchFilter("pId", SearchFilter.Operator.EQ, SysConst.ZERO_LONG));
        Specification<PmsAsProductClassify> specification = DynamicSpecifications.bySearchFilterByBasic(searchFilters);
        Sort orders = SortUtils.basicSort(
                new SortDto(SysConst.SortOrder.DESC.getCode(), "navStatus"),
                new SortDto(SysConst.SortOrder.DESC.getCode(), "showStatus"),
                new SortDto(SysConst.SortOrder.DESC.getCode(), "createTime"));
        return pmsAsProductClassifyRepository.findAll(specification, orders);
    }

    @Override
    public List<RoPmsAsProductClassify.ListWithChild> listWithChildren(List<PmsAsProductClassify> pmsAsProductClassifyList) {
        return pmsAsProductClassifyList.stream().map(item -> {
            RoPmsAsProductClassify.ListWithChild roPmsAsProductClassify = Conversion.poTurnRo(item, new RoPmsAsProductClassify.ListWithChild());
            List<PmsAsProductClassify> child = findByPId(item.getId());
            if (child.size() != SysConst.ZERO) {
                List<RoPmsAsProductClassify.ListWithChild> listWithChildren = listWithChildren(child);
                roPmsAsProductClassify.setListWithChild(listWithChildren);
            }
            return roPmsAsProductClassify;
        }).collect(Collectors.toList());
    }


}
